package com.iknow.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKLocationManager;
import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.db.IKnowDataBase;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.FriendAdapter;
import com.iknow.view.widget.MyListView;
import com.iknow.xmpp.service.Friend;
import com.iknow.xmpp.service.XmppBroadcastReceiver;
import com.iknow.xmpp.service.aidl.IKnowUser;
import com.iknow.xmpp.service.aidl.IXmppConnection;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.util.StringUtils;

public class SearchFriendsOptionActivity extends Activity
{
  private static final int ACTION_QUERY_BY_LBS = 12;
  public static final int ACTION_QUERY_BY_NAME = 11;
  private static final Intent SERVICE_INTENT = new Intent();
  private View.OnClickListener FindByLbsButtonListener;
  private View.OnClickListener FindByNickListener;
  private View.OnClickListener OnlineButtonListener;
  private AdapterView.OnItemClickListener listItemClickListener;
  private FriendAdapter mAdapter;
  private IKnow mApp;
  private BMapManager mBMapManager;
  private Button mBackButton;
  private boolean mBinded;
  private List<Friend> mFriendList;
  private FriendTask mFriendTask;
  private boolean mIsLbsSerch;
  private LinearLayout mLbsLayout;
  private MyListView mList;
  private View mListViewFooter;
  private View mListViewHeader;
  private RelativeLayout mLoadingBar;
  private BaiduLocationLisenter mLocationLisener;
  private MKLocationManager mLocationManager;
  private Location mMyLocation;
  private LinearLayout mNickNameLayout;
  private LinearLayout mOnlineLayout;
  private ProgressDialog mProgressDialog;
  private final XmppBroadcastReceiver mReceiver;
  private final ServiceConnection mServConn;
  private TaskListener mTaskListener;
  private IXmppFacade mXmppFacade;

  static
  {
    SERVICE_INTENT.setComponent(new ComponentName("com.iknow", "com.iknow.xmpp.service.IKnowXmppService"));
  }

  public SearchFriendsOptionActivity()
  {
    this.mServConn = new XmppServiceConnection(this, null);
    this.mReceiver = new XmppBroadcastReceiver();
    this.mBMapManager = null;
    this.FindByLbsButtonListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (SearchFriendsOptionActivity.access$0(this.this$0));
        while (true)
        {
          return;
          SearchFriendsOptionActivity.access$1(this.this$0, "正在获取位置，请稍候");
          SearchFriendsOptionActivity.access$2(this.this$0, true);
          this.this$0.initBaiduMap();
        }
      }
    };
    this.OnlineButtonListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (!(SearchFriendsOptionActivity.access$3(this.this$0)));
        while (true)
        {
          return;
          Intent localIntent = new Intent(this.this$0, OnlineUserActivity.class);
          this.this$0.startActivity(localIntent);
        }
      }
    };
    this.FindByNickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (!(SearchFriendsOptionActivity.access$3(this.this$0)));
        while (true)
        {
          return;
          Intent localIntent = new Intent(this.this$0, InforEditActivity.class);
          localIntent.putExtra("code", 11);
          this.this$0.startActivityForResult(localIntent, 11);
        }
      }
    };
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        Friend localFriend = SearchFriendsOptionActivity.access$4(this.this$0).getItem(paramInt - 1);
        if (localFriend == null);
        while (true)
        {
          return;
          Intent localIntent = new Intent(this.this$0, FriendActivity.class);
          localIntent.putExtra("friend", localFriend);
          this.this$0.startActivity(localIntent);
        }
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return null;
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        SearchFriendsOptionActivity.access$5(this.this$0).setVisibility(8);
        if (paramTaskResult == TaskResult.OK)
        {
          SearchFriendsOptionActivity.access$4(this.this$0).setList(SearchFriendsOptionActivity.access$6(this.this$0));
          SearchFriendsOptionActivity.access$4(this.this$0).notifyDataSetChanged();
        }
        while (true)
        {
          return;
          Toast.makeText(this.this$0, "网络超时，请稍后再试", 0).show();
        }
      }

      public void onPreExecute()
      {
        SearchFriendsOptionActivity.access$5(this.this$0).setVisibility(0);
      }
    };
  }

  private boolean checkIsOnline()
  {
    int i;
    if (!(this.mApp.isConnected()))
    {
      Toast.makeText(this, getString(2131361959), 0).show();
      i = 0;
    }
    while (true)
    {
      return i;
      i = 1;
    }
  }

  private void getRecommadedFriends()
  {
    if ((this.mFriendTask != null) && (this.mFriendTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      do
        return;
      while (this.mFriendList.size() > 0);
      this.mFriendTask = new FriendTask(this, null);
      this.mFriendTask.setListener(this.mTaskListener);
      this.mFriendTask.execute(new TaskParams[0]);
    }
  }

  private void initView()
  {
    setContentView(2130903125);
    this.mBackButton = ((Button)findViewById(2131296342));
    this.mBackButton.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.finish();
      }
    });
    findViewById(2131296344).setVisibility(8);
    this.mListViewHeader = getLayoutInflater().inflate(2130903126, null);
    this.mLbsLayout = ((LinearLayout)this.mListViewHeader.findViewById(2131296522));
    this.mLbsLayout.setOnClickListener(this.FindByLbsButtonListener);
    this.mNickNameLayout = ((LinearLayout)this.mListViewHeader.findViewById(2131296525));
    this.mNickNameLayout.setOnClickListener(this.FindByNickListener);
    this.mOnlineLayout = ((LinearLayout)this.mListViewHeader.findViewById(2131296520));
    this.mOnlineLayout.setOnClickListener(this.OnlineButtonListener);
    this.mAdapter = new FriendAdapter(this, getLayoutInflater());
    this.mList = ((MyListView)findViewById(2131296466));
    this.mList.addHeaderView(this.mListViewHeader);
    setupListViewFooter();
    this.mList.setAdapter(this.mAdapter);
    this.mList.setOnItemClickListener(this.listItemClickListener);
  }

  private void resetLBS()
  {
    if (this.mIsLbsSerch)
    {
      releaseBaiduMap();
      this.mIsLbsSerch = false;
    }
  }

  private void setupListViewFooter()
  {
    this.mListViewFooter = getLayoutInflater().inflate(2130903121, null);
    this.mLoadingBar = ((RelativeLayout)this.mListViewFooter.findViewById(2131296408));
    this.mList.addFooterView(this.mListViewFooter);
  }

  private void showProgressDialog(String paramString)
  {
    if (this.mProgressDialog != null)
    {
      this.mProgressDialog.dismiss();
      this.mProgressDialog = null;
    }
    this.mProgressDialog = new ProgressDialog(this);
    this.mProgressDialog.setCancelable(true);
    this.mProgressDialog.setMessage(paramString);
    this.mProgressDialog.show();
  }

  public void initBaiduMap()
  {
    this.mBMapManager = new BMapManager(this);
    this.mBMapManager.init(getString(2131361888), null);
    this.mLocationManager = this.mBMapManager.getLocationManager();
    this.mLocationManager.enableProvider(0);
    this.mLocationManager.enableProvider(1);
    this.mLocationLisener = new BaiduLocationLisenter(this, null);
    this.mBMapManager.start();
    this.mLocationManager.requestLocationUpdates(this.mLocationLisener);
  }

  protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent)
  {
    if ((paramInt1 == 11) && (paramInt2 == -1))
    {
      Intent localIntent = new Intent(this, NearByListActivity.class);
      localIntent.putExtra("lbs", false);
      localIntent.putExtra("nick", paramIntent.getAction());
      startActivity(localIntent);
    }
  }

  protected void onCreate(Bundle paramBundle)
  {
    requestWindowFeature(1);
    super.onCreate(paramBundle);
    initView();
    this.mApp = ((IKnow)getApplication());
    this.mFriendList = new ArrayList();
  }

  protected void onDestroy()
  {
    resetLBS();
    IKnow.mIKnowDataBase.deleteNearByFriend();
    this.mFriendList.clear();
    super.onDestroy();
  }

  protected void onPause()
  {
    resetLBS();
    if (this.mBinded)
    {
      this.mBinded = false;
      unbindService(this.mServConn);
    }
    super.onPause();
  }

  protected void onResume()
  {
    if (!(this.mBinded))
      this.mBinded = bindService(SERVICE_INTENT, this.mServConn, 1);
    super.onResume();
  }

  public void releaseBaiduMap()
  {
    this.mLocationManager.removeUpdates(this.mLocationLisener);
    this.mBMapManager.stop();
    this.mBMapManager.destroy();
    this.mBMapManager = null;
    this.mLocationManager = null;
  }

  private class BaiduLocationLisenter
  implements LocationListener
  {
    private boolean mRequestedFirstSearch = false;

    public void onLocationChanged()
    {
      if (!(this.mRequestedFirstSearch))
      {
        this.mRequestedFirstSearch = true;
        if (SearchFriendsOptionActivity.access$7(this.this$0) != null)
          SearchFriendsOptionActivity.access$7(this.this$0).dismiss();
        Intent localIntent = new Intent(this.this$0, NearByListActivity.class);
        localIntent.putExtra("lbs", true);
        localIntent.putExtra("lat", paramLocation.getLatitude());
        localIntent.putExtra("lon", paramLocation.getLongitude());
        this.this$0.startActivity(localIntent);
      }
    }
  }

  private class FriendTask extends GenericTask
  {
    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      Iterator localIterator;
      String str;
      Object localObject1;
      try
      {
        localIterator = IKnow.mApi.getrecommendedUsers().iterator();
        boolean bool = localIterator.hasNext();
        if (!(bool))
        {
          localTaskResult = TaskResult.OK;
          return localTaskResult;
        }
        str = (String)localIterator.next();
        Friend localFriend1 = IKnow.mIKnowDataBase.getFriendInfo(str);
        localObject1 = localFriend1;
        if (localObject1 == null);
      }
      catch (RemoteException localRemoteException1)
      {
        Object localObject3;
        try
        {
          if ((SearchFriendsOptionActivity.access$10(this.this$0).createConnection() != null) && (SearchFriendsOptionActivity.access$10(this.this$0).createConnection().getIKnowUser() != null))
          {
            Friend localFriend2 = SearchFriendsOptionActivity.access$10(this.this$0).createConnection().getIKnowUser().getUserInfo(StringUtils.parseName(str));
            localObject3 = localFriend2;
            if (localObject3 == null);
          }
        }
        catch (RemoteException localRemoteException1)
        {
          Object localObject2;
          try
          {
            localObject1 = new Friend(str, null, null);
            while (true)
            {
              while (true)
              {
                while (true)
                  SearchFriendsOptionActivity.access$6(this.this$0).add(localObject1);
                localException = localException;
                localTaskResult = TaskResult.FAILED;
              }
              IKnow.mIKnowDataBase.addFriend((Friend)localObject3);
              localObject1 = localObject3;
            }
            localRemoteException1 = localRemoteException1;
            localObject2 = localRemoteException1;
            ((RemoteException)localObject2).printStackTrace();
          }
          catch (RemoteException localRemoteException2)
          {
            while (true)
            {
              while (true)
              {
                localObject2 = localRemoteException2;
                localObject1 = localObject3;
              }
              localObject3 = localObject1;
            }
          }
        }
      }
    }
  }

  private class XmppServiceConnection
  implements ServiceConnection
  {
    public void onServiceConnected(, IBinder paramIBinder)
    {
      SearchFriendsOptionActivity.access$8(this.this$0, IXmppFacade.Stub.asInterface(paramIBinder));
      SearchFriendsOptionActivity.access$9(this.this$0);
    }

    public void onServiceDisconnected()
    {
      SearchFriendsOptionActivity.access$8(this.this$0, null);
    }
  }
}